/***************************************************************************
 *   This is a part of the NextCAD Web APP.                                *
 *   Copyright (C) AMCAX, All rights reserved.                             *
 ***************************************************************************/

// import { Alert, Button } from 'antd';
import { pickedVertexData } from '@amcax/base';
import { PickInputRJSFFormat } from '@app-cad/common/components/command-form/constants';
import { PickableCommandForm } from '@app-cad/common/components/command-form/pickable';
import { useCommand } from '@app-cad/common/hooks/useCommand';
import { useTools } from '@app-cad/common/hooks/useTools';
import { CurvedBaseCommand, CurvedCmdDialogParams } from '@app-cad/types';
import { RJSFSchema } from '@rjsf/utils';
import { useAsyncEffect } from 'ahooks';
import i18n from 'i18next';
import { useState } from 'react';

type Params = {
  points: string[];
};

function CmdFourPointSurface({ title, onClose, props }: CurvedCmdDialogParams) {
  const [formData, setFormData] = useState<pickedVertexData[]>([]);

  const schema: RJSFSchema = {
    type: 'array',
    items: {
      type: 'string',
      enum: [],
    },
    format: PickInputRJSFFormat.MultiPickVertex,
    uniqueItems: true,
  };

  const { picker } = useTools();

  const execute = async (preview = false) => {
    const params = {
      points: formData.map((i) => i.vertexId),
    };

    return confirmCommand(
      {
        type: '4point',
        a: {
          ...params,
        },
      },
      preview,
    );
  };

  useAsyncEffect(async () => {
    if (props != null) {
      const params: Params = props.params;
      const { points } = params;

      await initEditDependency(points);

      setFormData(
        points.map(
          (point) => picker.generatePickObjByPickID(point) as pickedVertexData,
        ),
      );
    }
  }, []);

  const isFormDataValid = () =>
    formData.length == 4 && formData.every((c) => c != null);

  const checkAndFinishDraw = async () => {
    if (!isFormDataValid()) {
      return;
    }

    await execute(false);
    onClose();
  };

  const { initEditDependency, initingEdit, clear, confirmCommand, isLoading } =
    useCommand('cmd-four-point-surface', {
      commandName: 'CreateSurface',
      editDxid: props?.shape,
      onClose,
      finishDraw: checkAndFinishDraw,
    });

  useAsyncEffect(async () => {
    if (!isFormDataValid()) {
      clear();
      return;
    }

    await execute(true);
  }, [formData]);

  return (
    <PickableCommandForm
      disabled={initingEdit}
      onClose={onClose}
      title={title}
      schema={schema}
      submitLoading={isLoading}
      onChange={(v) => {
        setFormData(v);
      }}
      submitText='完成'
      onSubmit={checkAndFinishDraw}
      extraUiSchema={{
        'ui:placeholder': '请输入四个端点',
        'ui:autofocus': true,
      }}
      formData={formData}
      isFormDataValid={isFormDataValid()}
      noValidate
    />
  );
}

export const cmdFourPointSurface: CurvedBaseCommand = {
  id: 'CmdFourPointSurface',
  label: 'command.four-point-surface',
  icon: 'commands/curved/four-point-surface',
  help: {
    remark: 'tips.redo',
  },
  create: (label, onClose, props) => (
    <CmdFourPointSurface
      title={i18n.t(label)}
      onClose={onClose}
      props={props}
    />
  ),
};
